<template>
	<view class="container">
		<view class="row">
			<view class="title">
				头像：
			</view>
			<view class="upload-box" @click="upload()">
				<image src="../../static/img/sctp.png" mode="aspectFill" v-if="avatar == ''"></image>
				<image :src="avatar" mode="aspectFit" v-else class="bg"></image>
			</view>
		</view>
		<view class="row">
			<view class="title">
				昵称：
			</view>
			<view class="input-box">
				<input type="text" :value="nick" @input="importNick" @blur="importNick" />
			</view>
		</view>
		<view class="row">
			<view class="title">
				公司名称：
			</view>
			<view class="input-box">
				<input type="text" :value="company" @input="importCompany" @blur="importCompany" />
			</view>
		</view>
		<view class="btn" @click="bind">
			绑定
		</view>
	</view>
</template>

<script>
	import comm from '../../static/data/commFunc.js'
	import cfg from '../../static/data/config.js'
	var r = cfg.router;
	export default {
		data() {
			return {
				avatar: "",
				nick: "",
				company: "",
				tempFilePaths: []
			};
		},
		onLoad() {
			console.log(comm.baseUrl)
			let data = {}
			comm.request("POST", "/app/member/getInfo", data, (res, data) => {
				console.log("个人信息", res)
				if (res.data.ret_code == 200) {
					// this.avatar = data.wxImgLogo
					this.nick = data.wxNickname
					this.company = data.company
				} else {
					comm.showToast(res.data.ret_msg, "none")
				}
			})
		},
		methods: {
			bind() {
				if (this.company == '') {
					comm.showToast("请输入公司名称", "none")
					return
				} else if (this.nick == '') {
					comm.showToast("请输入您的昵称", "none")
					return
				}
				let d = {
					company: this.company,
					wxNickname: this.nick
				}
				if(this.avatar == ''){
					comm.request("POST", "/app/member/editInfo", d, (res, data) => {
						console.log("修改资料", res)
						if (res.data.ret_code == 200) {
							comm.switchTab(r.my)
							comm.showToast("修改成功")
						} else {
							comm.showToast(res.data.ret_msg, "none")
						}
					})
					
				}else{
					uni.uploadFile({
						url: comm.baseUrl + '/app/member/editInfo',
						filePath: this.avatar,
						name: 'imgLogo',
						header: {
							'Content-Type': 'multipart/form-data;',
							'loginToken': comm.getCache("token")
						},
						formData: {
							data: JSON.stringify(d),
							isEncrypt: 'N',
						},
						success(res) {
							console.log(res)
							console.log(JSON.parse(res.data))
									if (JSON.parse(res.data).ret_code == 200) {
										comm.switchTab(r.my)
										comm.showToast("修改成功")
									} else {
										comm.showToast(JSON.parse(res.data).ret_msg, "none")
									}
						}
					})
				}
			},
			upload() {
				var that = this;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						console.log(JSON.stringify(res.tempFilePaths));
						console.log(res.tempFilePaths);
						that.avatar = res.tempFilePaths[0]
						const tempFilePaths = res.tempFilePaths
						that.tempFilePaths = res.tempFilePaths
						// uni.uploadFile({
						//      url: 'https://appshareairtest.jxwifi.com', //仅为示例，非真实的接口地址
						//      filePath: tempFilePaths[0],
						//      name: 'file',
						//      formData: {
						//      'loginToken': comm.getCache("token")
						//      },
						//      success (res){
						//        const data = res.data
						//        console.log(res.data)
						//      }
						//    })
					}
				});
			},
			importNick(e) {
				this.nick = e.detail.value
			},
			importCompany(e) {
				this.company = e.detail.value
			},
		},
	}
</script>

<style lang="less">
	.container {
		padding: 30rpx;
	}

	.title {
		width: 180rpx;
	}

	.row {
		justify-content: space-between;
	}

	.upload-box {
		flex: 1;
		margin-bottom: 20rpx;

		image {
			width: 242rpx;
			height: 168rpx;

		}

		.bg {
			background: #E5E5E5;
		}
	}

	.input-box {
		flex: 1;
		margin: 20rpx 0;
		border: 1rpx solid #E5E5E5;
		border-radius: 8rpx;
		padding: 10rpx 20rpx;

		input {
			width: 100%;
		}
	}

	.btn {
		width: 100%;
		text-align: center;
		padding: 25rpx 0;
		font-size: 28rpx;
		color: #FFFFFF;
		background: rgba(219, 48, 18, 1);
		border-radius: 40rpx;
		margin-top: 30rpx;
	}
</style>
